package rig.com.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author Rig
 * @create 2021/9/24 9:08
 **/
@Component
@SuppressWarnings(value = "all")
public class RedisUtils {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /*******
     * @description 设置key的过期时间
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key, long time) {
        try {
            redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 获取key的过期时间
     * @param key
     * @return
     */
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /*******
     * @description 判断key是否存在
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            return false;
        }
    }

    /**********
     * @description 删除key/删除多个key
     * @param key
     */
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
            }
        }
    }

    //=======================STRING

    /****
     * @description string获取key
     * @param key
     * @return
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /*****
     * @description 设置String key->value
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /******
     * @description 设置String key->value 并设置过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                this.set(key, value);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 如果不存在则设置String key->value
     * @param key
     * @param value
     * @return
     */
    public boolean setIfAbsent(String key, Object value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    /*****
     * @description 如果不存在则设置String并且设置过期时间 key->value
     * @param key
     * @param value
     * @return
     */
    public boolean setIfAbsent(String key, Object value, long time) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
    }

    /****
     * @description 如果存在则设置String key->value
     * @param key
     * @param value
     * @return
     */
    public boolean setIfPresent(String key, Object value) {
        return redisTemplate.opsForValue().setIfPresent(key, value);
    }

    /****
     * @description 如果存在则设置String并且设置过期时间 key->value
     * @param key
     * @param value
     * @return
     */
    public boolean setIfPresent(String key, Object value, long time) {
        return redisTemplate.opsForValue().setIfPresent(key, value, time, TimeUnit.SECONDS);
    }

    /*****
     * @description 获取到当前值并设置新值
     * @param key
     * @param value
     * @return
     */
    public Object getAndSet(String key, Object value) {
        return redisTemplate.opsForValue().getAndSet(key, value);
    }

    /******
     * @description 递增
     * @param key
     * @param delta
     * @return
     */
    public long incr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /******
     * @description 递增
     * @param key
     * @param delta
     * @return
     */
    public double incr(String key, double delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /******
     * @description 递减
     * @param key
     * @param delta
     * @return
     */
    public long decr(String key, long delta) {
        return redisTemplate.opsForValue().decrement(key, delta);
    }

    //==========================HASHMAP

    /*******
     * @description hash获取
     * @param key
     * @param item
     * @return
     */
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    /******
     * @description 根据key获取所有的键值
     * @param key
     * @return
     */
    public Map<Object, Object> hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /*******
     * @description 设置hashmap
     * @param key
     * @param map
     * @return
     */
    public boolean hmset(String key, Map<Object, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /********
     * @description 设置hashmap 并设置时间
     * @param key
     * @param map
     * @param time
     * @return
     */
    public boolean hmset(String key, Map<Object, Object> map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*******
     * @description 向指定的hash表添加数据  key - itme -> value
     * @param key
     * @param item
     * @param value
     * @return
     */
    public boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception a) {
            return false;
        }
    }

    /******
     * @description 向指定的hash表添加数据  key - itme -> value 并设置时间
     * @param key
     * @param item
     * @param value
     * @param time
     * @return
     */
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception a) {
            return false;
        }
    }

    /*****
     * @description 删除hash表的数据
     * @param key
     * @param item
     */
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }

    /*****
     * @description 判断hash表中是否存在该项item的值
     * @param key
     * @param item
     * @return
     */
    public boolean hHashKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }


    /*******
     * @description hash表中的自增
     * @param key
     * @param item
     * @param delta
     * @return
     */
    public long hincr(String key, String item, long delta) {
        return redisTemplate.opsForHash().increment(key, item, delta);
    }

    /*******
     * @description hash表中的自增
     * @param key
     * @param item
     * @param delta
     * @return
     */
    public double hincr(String key, String item, double delta) {
        return redisTemplate.opsForHash().increment(key, item, delta);
    }

    /******
     * @description hash表中的自减
     * @param key
     * @param item
     * @param delta
     * @return
     */
    public double hdecr(String key, String item, double delta) {
        return redisTemplate.opsForHash().increment(key, item, -delta);
    }

    //========================SET

    /*******
     * @description 根据key获取set的值
     * @param key
     * @return
     */
    public Set<Object> sget(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /*****
     * @description 判断集合的值是否存在
     * @param key
     * @param value
     * @return
     */
    public boolean sismember(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 向集合中添加元素
     * @param key
     * @param values
     * @return
     */
    public long sadd(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            return 0;
        }
    }

    /********
     * @description 向集合中添加元素并设置过期时间
     * @param key
     * @param time
     * @param values
     * @return
     */
    public long sadd(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0) {
                this.expire(key, time);
            }
            return count;
        } catch (Exception e) {
            return 0;
        }
    }

    /*****
     * @description 获取集合的长度
     * @param key
     * @return
     */
    public long slen(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    /*****
     * @description 删除集合的元素
     * @param key
     * @param values
     * @return
     */
    public long sremove(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key, values);
    }

    //==========================LIST

    /******
     * @description 获取队列内容
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<Object> lget(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /********
     * @description 设置指定队列索引的值
     * @param key
     * @param index
     * @param value
     * @return
     */
    public boolean lset(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*******
     * @description 获取队列长度
     * @param key
     * @return
     */
    public long llen(String key) {
        return redisTemplate.opsForList().size(key);
    }

    /*****
     * @description 根据索引获取
     * @param key
     * @param index
     * @return
     */
    public Object lindex(String key, long index) {
        return redisTemplate.opsForList().index(key, index);
    }

    /********
     * @description 向队尾添加元素
     * @param key
     * @param value
     * @return
     */
    public boolean lrightpush(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /********
     * @description 向队尾添加多个元素
     * @param key
     * @param value
     * @return
     */
    public boolean lrightpush(String key, Object... value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /********
     * @description 向队尾添加元素，并添加过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lrightpush(String key, Object value, long time) {
        try {
            Long aLong = redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /********
     * @description 向队尾添加多个元素，并添加过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lrightpush(String key, long time, Object... value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*******
     * @description 向队头添加元素
     * @param key
     * @param value
     * @return
     */
    public boolean lleftpush(String key, Object value) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*******
     * @description 向队头添加多个元素
     * @param key
     * @param value
     * @return
     */
    public boolean lleftpush(String key, Object... value) {
        try {
            redisTemplate.opsForList().leftPushAll(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 向队头添加元素，并设置过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lleftpush(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /********
     * @description 向队头添加多个元素，并设置过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lleftpush(String key, long time, Object... value) {
        try {
            redisTemplate.opsForList().leftPushAll(key, value);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 队列尾部弹出一元素
     * @param key
     * @return
     */
    public boolean lrightpop(String key) {
        try {
            redisTemplate.opsForList().rightPop(key);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 队列头部弹出一个元素
     * @param key
     * @return
     */
    public boolean lleftpop(String key) {
        try {
            redisTemplate.opsForList().leftPop(key);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*****
     * @description 删除count个值为vlaue的元素
     * @param key
     * @param count
     * @param value
     * @return
     */
    public long lremove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            return 0;
        }
    }
}
